package com.ailink.app;

import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.LinkedList;
import java.util.List;
import java.util.UUID;

/**
 * Created by Administrator on 2018/10/7
 * -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+PrintGCDateStamps -Xloggc:C:\Users\Administrator\Desktop\gc.log
 * -Djava.rmi.server.hostname=127.0.0.1
 * -Dcom.sun.management.jmxremote=true
 * -Dcom.sun.management.jmxremote.port=9004
 * 默认端口1099
 * -Dcom.sun.management.jmxremote.authenticate=false
 * -Dcom.sun.management.jmxremote.ssl=false
 * -Djava.net.preferIPV4Stack=true
 * -Dcom.sun.management.jmxremote=true
 * -Djava.rmi.server.hostname=192.168.238.129 -Dcom.sun.management.jmxremote.port=9000 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false
 */
@RestController
public class TestController {

//-Djava.rmi.server.hostname=127.0.0.1
//-Dcom.sun.management.jmxremote=true
//-Dcom.sun.management.jmxremote.port=9004
//-Dcom.sun.management.jmxremote.authenticate=false
//-Dcom.sun.management.jmxremote.ssl=false
//-Djava.net.preferIPV4Stack=true
//-Dcom.sun.managementote.ssl=false

    @RequestMapping("/home")
    public String home() {
        return "home";
    }

    private List<Person> personList = new LinkedList<>();

    /**
     * -Xmx32M -Xms32M -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=C:\Users\Administrator\Desktop
     */
    @RequestMapping("/oom")
    public void oom() {
        Integer i = 0;
        while (true) {
            i++;
            personList.add(new Person(i, UUID.randomUUID().toString()));
        }
    }

    @RequestMapping("/loop")
    public void loop() {
        while (true) {
//            try {
//                Thread.sleep(10);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
            System.out.println("loop");
        }
    }

    @RequestMapping("/sleep")
    public void sleep(@RequestParam("time") Double time) {
        try {
            Integer interval = (int)(time * 1000);
            System.out.println("sleep:" + interval);
            Thread.sleep(interval);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private Object lock1 = new Object();
    private Object lock2 = new Object();

    @RequestMapping("/lock")
    public void lock() {
        Thread t1 = new Thread() {
            @Override
            public void run() {
                synchronized (lock1) {
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    synchronized (lock2) {
                        System.out.println("t1 end");
                    }
                }
            }
        };

        Thread t2 = new Thread() {
            @Override
            public void run() {
                synchronized (lock2) {
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    synchronized (lock1) {
                        System.out.println("t2 end");
                    }
                }
            }
        };
        t1.start();
        t2.start();
    }

    public static class Person {

        private Integer id;

        private String name;

        public Person(Integer id, String name) {
            this.id = id;
            this.name = name;
        }
    }
}
